package cn.ehanghai.iot.back.controller;

import cn.ehanghai.iot.back.domain.UserInfo;
import cn.ehanghai.iot.common.controller.BaseController;
import cn.ehanghai.iot.config.ProjectUrlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 主页面
 * @author xumf
 * @date 2019/6/29 8:31
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    /**
     * 进入主页
     */
    @RequestMapping("/")
    public String welcome(@RequestParam String returnUrl, HttpServletRequest request){
        HttpSession session = request.getSession();
        UserInfo userInfo= (UserInfo) session.getAttribute("user_info");
        StringBuffer url = new StringBuffer();
        if(userInfo!=null){
            url.append(projectUrlConfig.getReturnUrl()+"?openid="+userInfo.getOpenId());
        }else {
            url.append(projectUrlConfig.getWechatMpAuthorize() + "/wechat/authorize");
            url.append("?returnUrl=" + returnUrl);
        }
        return "redirect:" +url;
    }


}
